package com.jxw.test.application.service.impl;

import com.jxw.boot.user.domain.DeviceUser;
import com.jxw.test.application.base.strategy.StrategySelector;
import com.jxw.test.application.base.strategy.StrategyType;
import com.jxw.test.application.base.strategy.handler.BaseStrategyHandler;
import com.jxw.test.application.domain.entity.DeviceBind;
import com.jxw.test.application.domain.model.DeviceBindRequest;
import com.jxw.test.application.service.DeviceService;
import org.springframework.stereotype.Service;

@Service
public class DeviceServiceImpl implements DeviceService {


    /**
     * 设备绑定业务
     * @param deviceBindRequest
     * @return
     */
    @Override
    public int deviceBind(DeviceBindRequest deviceBindRequest) {
        //=========1、解析设备信息（策略模式）=======
        DeviceUser deviceUser = parseDeviceInfo(deviceBindRequest);

        //=========2、创建设备绑定对象=======
        DeviceBind deviceBind = new DeviceBind();
        deviceBind.setUserId(deviceBindRequest.getUserInfo().getId());
        deviceBind.setDeviceId(deviceUser.getId());

        //========3、执行设备绑定（策略模式）========
        return doBind(deviceBind);
    }

    /**
     * 解析设备信息
     *
     * @param bindRequest
     * @return
     */
    @Override
    public DeviceUser parseDeviceInfo(DeviceBindRequest bindRequest) {
        //通过策略选择器，获取策略执行器对象
        // 参数1：业务参数
        // 参数2：策略类型
        // 返回值：具体的策略执行器对象
        BaseStrategyHandler<DeviceBindRequest, DeviceUser> parseHandler
                = StrategySelector.getStrategyHandler(bindRequest, StrategyType.DEVICE_INFO_PARSE);
        //执行核心业务流程
        return parseHandler.execute(bindRequest);
    }

    /**
     * 执行设备绑定操作
     *
     * @param deviceBind
     * @return
     */
    @Override
    public Integer doBind(DeviceBind deviceBind) {
        //通过策略选择器，获取策略执行器对象
        // 参数1：业务参数
        // 参数2：策略类型
        // 返回值：具体的策略执行器对象
        BaseStrategyHandler<DeviceBind, Integer> bindHandler
                = StrategySelector.getStrategyHandler(deviceBind, StrategyType.DEVICE_BIND);
        return bindHandler.execute(deviceBind);
    }


}
